package io.github.biezhi.wechat.utils;

import com.alibaba.fastjson.JSONObject;
import io.github.biezhi.wechat.voice.BaiduVoiceRequest;
import io.github.biezhi.wechat.voice.Token;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangsh
 * @date 2019-07-05 14:49
 */
public class BaiduVoiceUtils {


    public static final String VOICE_SPEED_URL = "https://vop.baidu.com/pro_api";

    public static final String VOICE_TTS_URL = "https://tsn.baidu.com/text2audio";

    public static final String GET_ACCESS_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token?";

    public static final String API_KEY = "DGGTBziZ1VaZ0XaYeGuSNzNm";
    public static final String APP_ID = "16719047";

    public static final String SECRET_KEY = "ZGrRE5USfQygkdzShRN4kAdRMkb63Fxl";

    public static final String MAC = "02:42:3e:c1:ec:d8";

    public static final String PCM = "PCM";



    public static String getAccessToken(){
        Map<String,String> map = new HashMap<>();
        map.put("grant_type","client_credentials");
        map.put("client_id",API_KEY);
        map.put("client_secret",SECRET_KEY);
        String json = HttpUtils.post(GET_ACCESS_TOKEN_URL, map);
        Token token = JSONObject.parseObject(json, Token.class);
        return token.getAccess_token();
    }


    public static BaiduVoiceRequest baiduVoiceRequest(String token,String speech,Integer len){
        BaiduVoiceRequest request = new BaiduVoiceRequest();
        request.setToken(token);
        request.setFormat(PCM);
        request.setCuid(MAC);
        request.setSpeech(speech);
        request.setLen(len);
        return request;
    }






}
